<%--
  Created by IntelliJ IDEA.
  User: 29688
  Date: 2021/4/11
  Time: 16:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/js/jquery.js"></script>
    <link rel="stylesheet" href="../../../layui-v2.5.7/layui/css/layui.css" media="all">
    <script src="../../../layui-v2.5.7/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-col-md12">
    <form class="layui-form">
        <div class="layui-form-item">
            <input name="bid" value="${bid}" hidden>
            <div class="layui-input-block" id="typeList">
                <%--书籍类型部分--%>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input class="layui-btn" lay-submit lay-filter="formDemo" type="button" value="提交">
            </div>
        </div>
    </form>
</div>

</body>
<script>

    var type =${types};
    var bookType =${bookType};
    function go() {
        if (type != null) {
            var html = '';
            for (let i = 0; i < type.length; i++) {
                for (let j = 0; j < bookType.length; j++) {
                    if (type[i].tid === bookType[j]) {
                        type[i].checked = "checked"
                    }
                }
            }
            for (let i = 0; i < type.length; i++) {
                html += '<input type="checkbox"  value= "' + type[i].tid + '" title="' + type[i].tname + '" ' + type[i].checked + '>'
            }
            console.log(html)
            $("#typeList").html(html)
        }

    }
    go()
    layui.use(['form','layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        form.on('submit(formDemo)', function(data){
            var bookTypes = []
            /*获取所复选框*/
            $('#typeList input[type=checkbox]:checked').each(function () {
                bookTypes.push(parseInt($(this).val()))
            })

            console.log(bookTypes)
            $.ajax({
                url:"/user/bookType/type",
                data:{"bookTypes":bookTypes,"bid":data.field.bid},
                type:"POST",
                traditional: true,
                success:function (data){
                    if(data>0){
                        layer.msg("修改成功！")
                    }
                }
            })
        });
    });
</script>
</html>
